﻿
using Microsoft.LightSwitch.Theming;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using System;
using System.Linq;
using Microsoft.VisualStudio.ExtensibilityHosting;
using ExtensionsMadeEasy.ClientAPI.Theme;

namespace ExtensionsMadeEasy.Presentation.Themes
{

      

    [Export(typeof(IThemeExtension))]
    [ThemeExtension(EasyTheme.ThemeId)]
    public class EasyThemeExtension :  IThemeExtension
    {
        internal readonly IList<Uri> EmptyList = new System.Collections.ObjectModel.ReadOnlyCollection<Uri>(new List<Uri>());

        private bool initialized = false;
        private IThemeExtension actualThemeExtension;

        private IThemeExtension ActualThemeExtension
        {
            get
            {
                if (!initialized)
                {
                    var allThemeExtensionExporters = VsExportProviderService.GetExportedValues<IThemeExtensionExporter>();
                    var themeExtensionExporter = allThemeExtensionExporters.FirstOrDefault();
                    if (themeExtensionExporter != null) {
                        actualThemeExtension = themeExtensionExporter.GetThemeExtension();
                    }
                    initialized = true;
                }
                return actualThemeExtension;
            }
        }


        public System.Collections.Generic.IEnumerable<System.Uri> GetControlStyleResources(string themeId, string themeVersion, System.Collections.Generic.IEnumerable<Microsoft.LightSwitch.Model.IModuleDefinition> modules)
        {
            return ActualThemeExtension != null ?
                ActualThemeExtension.GetControlStyleResources(themeId, themeVersion, modules) :
                EmptyList;
        }
    }
}
